#!usr/bin/env python
# -*- coding: utf-8 -*-
import frida, sys

def on_message(message, data):
    print(message)
    #for each in message['payload']:
    #    print(each)
    #print("[on_message] message:", message, "data:", data)
#processe#s = frida.get_remote_device().enumerate_processes()
#for process in processes:
#    print(process)
front_app = frida.get_remote_device().get_frontmost_application()
session = frida.get_remote_device().attach(front_app.pid)


jscode = """  
Java.perform(function() {
    var stack = Java.use("com.bytedance.android.livesdk.gift.platform.core.GiftService");
    dumpAllFieldValue(stack.$new());
}); 
function dumpAllFieldValue(obj) {
    if (obj === null) {
        return;
    }
    console.log("======================Dump all f method======================");
    var methods = obj.class.getDeclaredMethods();
    for(var j = 0; j < methods.length; j++){
    
        var methodName = methods[j].getName();
        console.log("----"+methodName); 
       // methods[j].implementation = function(args) {
        //    console.log(args); 
       // } 
    } 
    //obj.class.method.implementation = function(args) {
    //     console.log(args); 
    //}
    console.log("===========Dump all fields value for  " + obj.getClass() + " :");

    var cls = obj.getClass();

    while (cls !== null && !cls.equals(Java.use("java.lang.Object").class)) {
        var fields = cls.getDeclaredFields();
        if (fields === null || fields.length === 0) {
            cls = cls.getSuperclass();
            continue;
        }

        if (!cls.equals(obj.getClass())) {
            console.log("Dump super class  " + cls.getName() + " fields:");
        }

        for (var i = 0; i < fields.length; i++) {
            var field = fields[i];
            field.setAccessible(true);
            var name = field.getName();
            var value = field.get(obj);
            var type = field.getType();
            console.log(type + " " + name + "=" + value);
        }

        cls = cls.getSuperclass();
    }
}
"""

script = session.create_script(jscode)

script.on("message", on_message)
script.load()
"""
Java.choose("com.bytedance.android.livesdk.chatroom.model.LiveRoomIDData",{
            onMatch:function(instance){
                console.log("instance.toString:",instance.toString());
                if(instance.toString().indexOf("ISBN")!= -1){
                    console.log("instance.toString:",instance.toString());
                }
            },onComplete:function(){
                console.log("search complete!")
            }
        })"""